import { getControlNetwork } from '@/services/zhirong/controlPower.js';
import { useRequest } from '@umijs/hooks';
import { message, Table } from 'antd';
import { nanoid } from 'nanoid';
import { useEffect, useState } from 'react';

const ControlPowerList = (props) => {
  const sendControl = props.getControlPower();
  const [data, setData] = useState();
  const [enterprise, setEnterprise] = useState(props.enterprise);
  const columns = [
    {
      title: '序号',
      render: (text, record, index) =>
        `${(pageOption.pageNo - 1) * pageOption.pageSize + (index + 1)}`,
      width: '10%',
      key: 'num'
    },
    {
      title: '股东名称',
      dataIndex: 'name',
      width: '70%',
      textWrap: 'word-break',
      ellipsis: true,
      key: "holdersName"
    },
    {
      title: '控制权比例',
      dataIndex: 'controlPower',
      width: '20%',
      render: (text, record, index) => (text * 100).toFixed(2) + '%',
      defaultSortOrder: 'descend',
      sortDirections: ['descend', 'ascend'],
      sorter: (a, b) => a.controlPower - b.controlPower,
      key: "percentOfControl"
    },
  ];

  const { loading, run } = useRequest(getControlNetwork, {
    manual: true,
    onSuccess: (result) => {
      console.log('控制权', result);
      console.log(result);
      // 给父级传控制权数据
      props.getControlPower(result);
      setData(result.data.nodes);
    },
    onError: () => {
      message.error('数据请求错误！');
    },
  });
  //数据源
  const dataSource = data;
  useEffect(() => {
    run({
      centerId: enterprise.eid,
    });
  }, [enterprise]);

  //分页默认值
  const [pageOption, setPageOption] = useState({
    pageNo: 1,
    pageSize: 10,
  });

  //分页配置
  const paginationProps = {
    current: pageOption.pageNo,
    pageSize: pageOption.pageSize,
    onChange: (current, size) => paginationChange(current, size),
  };

  const paginationChange = async (current, size) => {
    setPageOption({
      pageNo: current,
      pageSize: size,
    });
  };
  return (
    <>
      <Table
        size="small"
        dataSource={dataSource}
        columns={columns}
        pagination={paginationProps}
        rowKey={nanoid()}
      ></Table>
    </>
  );
};

export default ControlPowerList;
